﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DatabaseHandler
{
    public class DatabaseWarehouseHandler
    {
        public DatabaseWarehouseDataContext db = new DatabaseWarehouseDataContext();

        public static DatabaseWarehouseHandler instance = new DatabaseWarehouseHandler(); //instancja wykorzystywana przez Server

        private DatabaseWarehouseHandler() { } //konstruktor

        public void RegisterUser(User _user)
        {
            _user.Privileges = "user";
            db.Users.InsertOnSubmit(_user);
            db.SubmitChanges();
        }

        public void AddUser(User _user)
        {
            db.Users.InsertOnSubmit(_user);
            db.SubmitChanges();
        }

        public void DeleteUser(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Name == _user.Name
                 select a).Single();

            db.Users.DeleteOnSubmit(user);
            db.SubmitChanges();
        }

        public void EditUser(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Name == _user.Name
                 select a).FirstOrDefault();

            User temp = new User();

            temp.ID = user.ID;
            temp.Name = user.Name;
            temp.Password = user.Password;
            temp.Privileges = user.Privileges;
            if (_user.Password != null) user.Password = _user.Password;
            else user.Password = temp.Password;
            if (_user.Privileges != null) user.Privileges = _user.Privileges;
            else user.Privileges = temp.Privileges;
            
            db.SubmitChanges();
        }

        public bool CheckUserExist(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Name == _user.Name
                 select a).FirstOrDefault();

            if (user == null) return false;
            else return true;
        }

        public bool CheckUserPass(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Name == _user.Name
                 select a).FirstOrDefault();

            if (user.Password == _user.Password) return true;
            else return false;
        }

        public bool CheckIfAdmin(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Name == _user.Name
                 select a).FirstOrDefault();

            if (user.Privileges == "admin") return true;
            else return false;
        }

        public List<User> GetAllUsers()
        {
            return db.Users.ToList();
        }

        public List<User> GetUsersByPrivileges(User _user)
        {
            var user =
                (from a in db.Users
                 where a.Privileges == _user.Privileges
                 select a).ToList();

            return user;
        }

        public void AddProduct(Product _product)
        {
            db.Products.InsertOnSubmit(_product);
            db.SubmitChanges();
        }

        public void DeleteProduct(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).Single();

            db.Products.DeleteOnSubmit(product);
            db.SubmitChanges();
        }

        public bool CheckProductExist(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).FirstOrDefault();

            if (product == null) return false;
            else return true;
        }

        public void EditProduct(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).FirstOrDefault();

            Product temp = new Product();

            temp.ID = product.ID;
            temp.Name = product.Name;
            temp.Category = product.Category;
            temp.Price = product.Price;
            temp.Quantity = product.Quantity;
            if (_product.Price != 0) product.Price = _product.Price;
            else product.Price = temp.Price;
            if (_product.Quantity != null) product.Quantity = _product.Quantity;
            else product.Quantity = temp.Quantity;

            db.SubmitChanges();
        }

        public void BuyProduct(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).FirstOrDefault();

            Product temp = new Product();
            temp.ID = product.ID;
            temp.Name = product.Name;
            temp.Category = product.Category;
            temp.Price = product.Price;
            temp.Quantity = product.Quantity;
            product.Quantity = temp.Quantity - _product.Quantity;
            db.SubmitChanges();
        }


        public double GetPrice(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).FirstOrDefault();

            return product.Price;
        }

        public bool CheckProductQuantity(Product _product)
        {
            var product =
                (from a in db.Products
                 where a.Name == _product.Name
                 select a).FirstOrDefault();

            if (product.Quantity < _product.Quantity) return false;
            else return true;
        }

        public List<Product> GetAllProducts()
        {
            return db.Products.ToList();
        }

        public List<Product> GetProductsByCategory(Product _product)
        {
            var product =
                (from a in db.Products
                where a.Category == _product.Category
                select a).ToList();

            return product;
        }

        static void Main(string[] args)
        {
            while (true) { };
        }
    }
}
